package br.ufmg.notservice.activity;

import br.ufmg.notservice.R;
import br.ufmg.notservice.service.EventReceiver;
import br.ufmg.notservice.service.NotificationEventServer;
import br.ufmg.notservice.service.NotificationIntent;
import br.ufmg.notservice.service.NotificationReceiver;
import br.ufmg.notservice.utils.AndroidUtils;
import br.ufmg.ubicomp.event.EventObject;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class NotificationComponent extends Activity implements OnClickListener, NotificationReceivable {
	
	private TextView textView;
	private Button ok;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView = (TextView) findViewById(R.id.text);
        ok = (Button) findViewById(R.id.button);
        ok.setOnClickListener(this);
        initializeReceivers();
		NotificationEventServer.getInstance().addNotificationListener(this);
    }

	private void initializeReceivers() {
		NotificationReceiver notReceiver = new NotificationReceiver();
        IntentFilter filter = new IntentFilter(NotificationConstants.NOTIFICATION_RECEIVER);
        getApplicationContext().registerReceiver(notReceiver, filter);
        EventReceiver eventReceiver = new EventReceiver();
        IntentFilter filter2 = new IntentFilter(NotificationConstants.EVENT_RECEIVER);
        getApplicationContext().registerReceiver(eventReceiver, filter2);
	}

	public void onClick(View arg0) {
		AndroidUtils.showAlert(this, "sending event");
		Intent ni = new Intent(NotificationConstants.EVENT_RECEIVER);
		ni.putExtra(NotificationConstants.EVENT_MESSAGE, "ok");
		getApplicationContext().sendBroadcast(ni);
	}

	public void showNotification(Intent intent) {
		
		String text = intent.getStringExtra(NotificationConstants.NOTIFICATION_MESSAGE);
		AndroidUtils.showNotification(this, "Notification", text, this.getClass().getSimpleName());
		EventObject eo = NotificationEventServer.getInstance().getEvent(intent.getLongExtra(NotificationConstants.EVENT_ID, 0));
		if (eo != null)
		{
			Log.i(getClass().getSimpleName(), eo.getName());
		}
		//AndroidUtils.showIntent(intent);
	}
	
}